package edu.uoc.tds.pac4.client.ss1;

import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.client.ClientInterface.TipusPantalla;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.beans.Centre;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

/**
 * Pantalla que dona de alta Centres
 */
public class PantallaAltaCentres 
	extends PantallaCentres implements PanelClientInterface{
	
	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;
	private JPanelDadesCentre jPanelCentre = null;
	private JButton jButtonGuardar = null;
	private JButton jButtonCancelar = null;
	private Client cliente = null;
	
	/**
	 * Constructor
	 */
	public PantallaAltaCentres() {
		super();
		initialize();
	}
	
	/**
	 * Constructor que rep una referencia al servidor remot
	 * 
	 * @param remot
	 */
	public PantallaAltaCentres(Client cliente,  RemoteInterface remot) {
		this.cliente = cliente;
		this.remot = remot;
		initialize();
	}

	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize() {
		// this.setTitle("Pantalla.title");
	}
	
	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {

			GridBagConstraints gbcPanelCentre = new GridBagConstraints();
			gbcPanelCentre.gridx = 1;
			gbcPanelCentre.gridy = 1;
			gbcPanelCentre.weightx = 1;
			gbcPanelCentre.weighty = 1;
			gbcPanelCentre.insets = new Insets(0, 10, 10, 10);
			gbcPanelCentre.fill = GridBagConstraints.BOTH;

			jPanelContainer = new JPanel();
			jPanelContainer.setSize(dimension);
			jPanelContainer.setLayout(new GridBagLayout());
			
			jPanelContainer.add(getjPanelCentre(), gbcPanelCentre);
			
			jPanelContainer.add(getJButtonGuardar(), null);
			jPanelContainer.add(getJButtonCancelar(), null);
		}
		return jPanelContainer;
	}
	
	/**
	 * This method initializes jPanelUsuari
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getjPanelCentre() {
		if (jPanelCentre == null) {
			jPanelCentre = new JPanelDadesCentre(this);
		}
		return jPanelCentre;
	}
	
	/**
	 * This method initializes JButtonGuardar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGuardar() {

		if (jButtonGuardar == null) {
			jButtonGuardar = new JButton("Guardar");
			jButtonGuardar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonGuardar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						guardar();

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonGuardar;
	}

	private void guardar (){
		Centre centre = jPanelCentre.GetCentre();
		if(centre != null)
			try {
				remot.insertUpdateCentre(centre, true);
				new PantallaError("Info_guardat", PantallaError.Tipus.INFO);    
				cliente.carregaPantalla(TipusPantalla.ModifBaixaCentres);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (GestioEventsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton("Cancelar");
			jButtonCancelar.setBounds(new Rectangle(240, 195, 100, 25));
			jButtonCancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					cliente.carregaPantalla(TipusPantalla.ModifBaixaCentres);
				}
			});
		}
		return jButtonCancelar;
	}		
}
